const userService = require('../service/user_service')
const { NAME_OR_PASSWORD_IS_REQUIRED, NAME_IS_ALREADY_EXISTS} =require('../config/error')
const md5password = require('../utils/md5-password')
const verifyUser = async (ctx, next) => {
    const { name, password } = ctx.request.body
    //2.1判断用户是否为空
    if (!name || !password) {
        return ctx.app.emit('error',NAME_OR_PASSWORD_IS_REQUIRED,ctx)
    }
    //2.2 判断name是否存在数据库中
    const users = await userService.findUserByName(name)
    if (users.length) {
        return ctx.app.emit('error',NAME_IS_ALREADY_EXISTS,ctx)
     
    }


    await next()

}
const handlePassword = async (ctx, next) => {
  // 1.取出密码
  const { password } = ctx.request.body

  // 2.对密码进行加密
  ctx.request.body.password = md5password(password)

  // 3.执行下一个中间件
  await next()
}

module.exports = {
    verifyUser,
    handlePassword
}